package com.ylb.rent.controller;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ylb.rent.util.MinioUtil;
import com.ylb.rent.util.R;
import io.minio.errors.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;

import com.ruoyi.common.enums.BusinessType;
import  com.ylb.rent.entity.File;
import com.ylb.rent.service.IFileService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件Controller
 * 
 * @author ylb
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/file/file")
public class FileController
{
    @Autowired
    private IFileService fileService;


    @Autowired
    MinioUtil minioUtil;



    @PostMapping("/upload")
    public String upload(@RequestBody MultipartFile file){

        try {
            String image = minioUtil.put(file);
            return R.successJSONData("成功",image);
        } catch (Exception e) {
            e.printStackTrace();
            return R.errorJSONData("失败");
        }

    }


    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('file:file:list')")
    @GetMapping("/list")
    public String list(File file)
    {

        List<File> list = fileService.selectFileList(file);
        return R.successJSONData(list);
    }


    /**
     * 获取文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:file:query')")
    @GetMapping(value = "/{id}")
    public String getInfo(@PathVariable("id") Long id)
    {
        return R.successJSONData(fileService.selectFileById(id));
    }

    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermi('file:file:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public String add(@RequestBody File file)
    {
        return R.successJSONData(fileService.insertFile(file));
    }

    /**
     * 修改文件
     */
    @PreAuthorize("@ss.hasPermi('file:file:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public String edit(@RequestBody File file)
    {
        return R.successJSONData(fileService.updateFile(file));
    }

    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermi('file:file:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public String remove(@PathVariable Long[] ids)
    {
        return R.successJSONData(fileService.deleteFileByIds(ids));
    }
}
